﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hack.DataStructures
{
    public class LambdaComparer<T,S>:IComparer<T>, IEqualityComparer<T>
    {
        Func<T, S> func;
        IComparer<S> comparer = Comparer<S>.Default;
        IEqualityComparer<S> equalityComparer= EqualityComparer<S>.Default; 

        public LambdaComparer(Func<T, S> func)
        {
            if (func == null)
                throw new ArgumentNullException("func");

            this.func = func;
        }

        public int Compare(T x, T y)
        {
            return comparer.Compare(func(x), func(y)); 
        }

        public bool  Equals(T x, T y)
        {
 	        return equalityComparer.Equals(func(x), func(y)); 
        }

        public int  GetHashCode(T obj)
        {
 	        return equalityComparer.GetHashCode(func(obj)); 
        }
    }


}
